﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Auditing;
using WisDom.SmartTMS.Enums;

namespace WisDom.SmartTMS.Entity
{
    /// <summary>
    /// 审核主记录（聚合根）
    /// </summary>
    public class Audit: CreationAuditedAggregateRoot<Guid>
    {
        /// <summary>
        /// 业务编号ID（对应例如合同的主键ID）
        /// </summary>
        public Guid BusinessId { get; private set; }

        /// <summary>
        /// 业务类别编号ID
        /// </summary>
        public BusinessType BusinessTypeCode { get; private set; }

        /// <summary>
        /// 最终审核结果
        /// </summary>
        public AuditState State { get; private set; }

        /// <summary>
        /// 最后审核结束时间
        /// </summary>
        public DateTime? AuditTime { get; private set; }

        /// <summary>
        /// 审批人（最近的那个审核人）
        /// </summary>
        public string AuditPersonName { get; set; }
        
        /// <summary>
        /// 审核日志
        /// </summary>
        public List<AuditLog>  AuditLogs { get; private set; }
        
        /// <summary>
        /// 默认构造方法
        /// </summary>
        protected Audit()
        {
        }

        /// <summary>
        /// 构造新实体对象
        /// </summary>
        public Audit(Guid id, Guid businessId, BusinessType businessType, DateTime creationTime, Guid? creatorId)
        {
            AuditLogs = new List<AuditLog>(); //实例化审核日志集合

            Id = id;                          //主键
            BusinessId = businessId;          //业务id
            BusinessTypeCode = businessType;  //业务类型
            CreationTime = creationTime;      //创建时间
            CreatorId = creatorId;            //创建人

            State = AuditState.ToAudit;       //新增时，默认为"待审核"
            AuditTime = null;                 //审核时间默认为空
        }

        /// <summary>
        /// 添加审批日志，即添加新的审批人
        /// </summary>
        /// <param name="logId">主键</param>
        /// <param name="processPersonId">审核人</param>
        /// <param name="processPersonName">审核人姓名</param>
        /// <param name="isDepBoss">是否审核人的部门领导</param>
        public void AddAuditLog(Guid logId, Guid processPersonId, string processPersonName)
        {
            //实例化一条审计日志
            AuditLog auditLog = new(logId, this.Id, processPersonId, processPersonName);

            //添加到审计日志集合中
            AuditLogs.Add(auditLog);
        }

        /// <summary>
        /// 查找审批主记录下需要当前审批人的审核日志
        /// </summary>
        /// <param name="personId"></param>
        /// <returns></returns>
        public AuditLog GetAuditLogByPersonId(Guid personId)
        {
            //条件查询
            AuditLog auditLog = AuditLogs.Find(al => al.ProcessPersonId == personId  //审批人
                                             && al.AuditId == Id);                   //主记录Id必须相同
            return auditLog;
        }

        /// <summary>
        /// 通过并终止审批或拒绝
        /// </summary>
        /// <param name="personId">当前审批人</param>
        /// <param name="state">审批结果(通过或终止)</param>
        /// <param name="remark">审批备注</param>
        public void EndAudit(Guid personId, string processPersonName, AuditState state, string remark)
        {
            //查找需要审批的日志
            AuditLog auditLog = GetAuditLogByPersonId(personId);

            //修改审批状态
            auditLog.UpdateAuditLog(state, remark);
            
            this.State = state;                       //修改主表最终状态
            this.AuditTime = DateTime.Now;            //修改主表最终审批时间
            this.AuditPersonName = processPersonName; //修改主表最终人
        }

        /// <summary>
        /// 审批通过但不终止审批
        /// </summary>
        /// <param name="personId">当前审批人</param>
        /// <param name="processPersonName">当前审批人姓名</param>
        /// <param name="remark">当前审批人备注</param>
        public void ProcessAudit(Guid personId, string processPersonName, string remark)
        {
            //查找属于当前审批人的审批日志
            AuditLog auditLog = GetAuditLogByPersonId(personId);

            //当前审批人通过审批
            auditLog.UpdateAuditLog(AuditState.Approved, remark);

            this.State = AuditState.InAudit;          //审批主表状态修改为"审核中"
            this.AuditPersonName = processPersonName; //修改审批主表的最新审批人
        }
    }
}
